import { FileLibraryType } from "@/type/config";
import AliyunOSS from "./Aliyun";
import FileOSS from "./Local";

interface OSSConfig {
	libraryRoot: string; // 文件服务地址
	libraryBucket: string; // Bucket名称
	libraryType: FileLibraryType; // 文件库类型
	region?: string; // region
	maxFileSize?: number; // 超出最大文件就分片（本地OSS）
	params?: any; // 旋转、缩放等参数
}

export interface IClient {
	upload: (file: File, fileFullPath: string) => Promise<{ fileId: string }>;
	download: (fileFullName: string, isOriginal?: boolean) => Promise<string>;
	downloadStream: (fileFullName: string) => Promise<Blob>;
	downloadZip: (fileFullName: string, fileName: string) => Promise<string>; // 下载打包文件
	isExist: (fileFullName: string) => Promise<boolean>;
}

export default class OSSTools {
	static getClient(config: OSSConfig) {
		if (config.libraryType == FileLibraryType.本地oss) {
			return new FileOSS(
				config.libraryRoot,
				config.libraryBucket,
				config.maxFileSize || 5 * 1024 * 1024
			);
		} else {
			if (!config.region) {
				throw new Error("当前选择存储空间为阿里云，请传入region");
			}
			return new AliyunOSS(
				config.libraryRoot,
				config.region,
				config.libraryBucket
			);
		}
	}
}
